package org.springframework.beans.factory.test.annoTest;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.anno.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/12/19
 */
//@Component
public class MyFactoryBean implements FactoryBean<Object> {
    private Class<?> clazz;
    public MyFactoryBean(Class<?> clazz){
        System.out.println(clazz);
        this.clazz = clazz;
    }

    @Override
    public Object getObject(){
        return Proxy.newProxyInstance(clazz.getClassLoader(),new Class[]{clazz},new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println(method.getName());
                Select select = method.getAnnotation(Select.class);
                if(null!=select)
                    System.out.println(select.value());
                if(null!=args)
                    System.out.println(args[0]);
                return new User(1,"lry");
            }
        });
    }

    @Override
    public Class<?> getObjectType() {
        return clazz;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
